package com.zshan.clinic.common.util.validate;

import java.util.regex.Pattern;

public class EmailValidator {

    // 经典邮箱正则表达式（匹配常见邮箱格式）
    private static final String EMAIL_REGEX =
            "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";

    private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);

    /**
     * 验证邮箱格式是否合法
     * @param email 邮箱地址
     * @return true 合法，false 不合法
     */
    public static boolean isValidEmail(String email) {
        if (email == null || email.length() > 254) {
            return false;
        }
        return EMAIL_PATTERN.matcher(email).matches();
    }

    // 示例测试
    public static void main(String[] args) {
        String email1 = "371081483@qq.com";
        String email2 = "user.name+alias@sub.domain.co";
        String email3 = "invalid_email@";
        String email4 = "中文@邮箱.com";

        System.out.println("邮箱1合法: " + isValidEmail(email1)); // true
        System.out.println("邮箱2合法: " + isValidEmail(email2)); // true
        System.out.println("邮箱3合法: " + isValidEmail(email3)); // false
        System.out.println("邮箱4合法: " + isValidEmail(email4)); // false
    }
}

